package com.xbd.flex.code;

import com.alibaba.druid.pool.DruidDataSource;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.xbd.flex.base.BaseEntity;

public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/flex?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //基础包路径
        globalConfig.setBasePackage("com.xbd.flex");
        globalConfig.setMapperXmlPath(globalConfig.getSourceDir() + "/../resources/mapper");

        //设置只生成哪些表
        globalConfig.setGenerateTable("test");

        //entity
        globalConfig
            .enableEntity()
            .setWithLombok(true)
            .setSupperClass(BaseEntity.class)
            .setImplInterfaces();

        //mapper
        globalConfig.enableMapper();

        //mapper.xml
        globalConfig.enableMapperXml();

        //service
        globalConfig.enableService().setClassPrefix("I");
        globalConfig.enableServiceImpl();

        //controller
        globalConfig.enableController();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }
}
